package com.micro.glszh.stat.controller;

import com.micro.glszh.stat.base.CommonResult;
import com.micro.glszh.stat.config.Const;
import com.micro.glszh.stat.domain.SysUser;
import com.micro.glszh.stat.domain.vo.UserPassVo;
import com.micro.glszh.stat.interceptor.AuthCheck;
import com.micro.glszh.stat.service.SysUserService;
import com.micro.glszh.stat.utils.PageResult;
import com.micro.glszh.stat.utils.RsaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * SysUserController
 *
 * @Author gaopengfei
 * @create 2024/10/21 16:10
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "用户管理")
@RequestMapping("/api/sysUser")
public class SysUserController {

    private final SysUserService sysUserService;

    @AuthCheck
    @ApiOperation("分页获取用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页，默认第一页", defaultValue = "1", required = false),
            @ApiImplicitParam(name = "size", value = "每页多少条，默认10条", required = false),
            @ApiImplicitParam(name = "phone", value = "电话号码", required = false),
            @ApiImplicitParam(name = "password", value = "登录密码", required = false)
    })
    @GetMapping("/list")
    public CommonResult<PageResult<SysUser>> listUser(Integer page, Integer size, String name, String phone){
        return CommonResult.success(sysUserService.list(page, size, name, phone));
    }

    @AuthCheck
    @ApiOperation("新增或修改用户")
    @PostMapping()
    public CommonResult<Boolean> insertOrUpdateUser(@Validated @RequestBody SysUser sysUser) throws Exception {
        if (Objects.isNull(sysUser.getId())) {
            sysUser.setPassword(RsaUtils.encryptByPublicKey(Const.PUBLIC_KEY, "123456"));
        }
        return CommonResult.success(sysUserService.saveOrUpdate(sysUser));
    }

    @AuthCheck
    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public CommonResult<Boolean> deleteUser(@PathVariable Integer id){
        return CommonResult.success(sysUserService.removeById(id));
    }

    @AuthCheck
    @ApiOperation("重置密码")
    @PostMapping("/{id}")
    public CommonResult<Boolean> resetPassword(@PathVariable Integer id){
        return CommonResult.success(sysUserService.resetPassword(id));
    }

    @AuthCheck
    @ApiOperation("修改密码")
    @PostMapping(value = "/updatePass")
    public CommonResult<Boolean> updatePass(@RequestBody UserPassVo passVo) throws Exception {
        return CommonResult.success(sysUserService.updatePass(passVo));
    }

    @GetMapping("/login")
    @ApiOperation(value = "小程序登录", notes = "小程序登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话号码", required = true),
            @ApiImplicitParam(name = "password", value = "登录密码", required = true)
    })
    public CommonResult<Map<String, Object>> login(String phone, String password) {
        return CommonResult.success(sysUserService.login(phone, password));
    }

}
